/**
 * @description: 这是Eirudy创建的类~
 * @author: Eirudy
 * @hope: 憧憬成为java大师
 **/

package org.eirudy.shareimage.manager;

import cn.hutool.core.io.FileUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.demo.PicOperationDemo;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import org.eirudy.shareimage.config.CosClientConfig;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CosManager {

    @Resource
    private CosClientConfig cosClientConfig;

    @Resource
    private COSClient cosClient;

    // 将本地文件上传到 COS
    public PutObjectResult putObject(String key, File file)
            throws CosClientException, CosServiceException {
       PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);
        return cosClient.putObject(putObjectRequest);
    }

    public COSObject getObject(String key)
            throws CosClientException, CosServiceException{
        GetObjectRequest getObjectRequest = new GetObjectRequest(cosClientConfig.getBucket(), key);
        return cosClient.getObject(getObjectRequest);
    }

    /**
     *
     * @param key 唯一键，文件路径
     * @param file 文件本身
     * @return
     * @throws CosClientException
     * @throws CosServiceException
     */
    //上传并解析图片
    public PutObjectResult putPictureObject(String key, File file)
            throws CosClientException, CosServiceException {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);
        PicOperations picOperations = new PicOperations();
        picOperations.setIsPicInfo(1);


        List<PicOperations.Rule> rules = new ArrayList<>();
        //图片压缩，转换成(webp格式)
        String webpKey = FileUtil.mainName(key)+".webp";
        PicOperations.Rule compressRule = new PicOperations.Rule();
        compressRule.setRule("imageMogr2/format/webp");
        compressRule.setFileId(webpKey);
        compressRule.setBucket(cosClientConfig.getBucket());
        rules.add(compressRule);

        //缩略图处理
        if(file.length() > 20 * 1024L){
        String thumbKey = FileUtil.mainName(key)+"_thumbnail." + FileUtil.getSuffix(key);
        PicOperations.Rule thumbnailRule = new PicOperations.Rule();
        //如果大于原图宽高，就不处理
        thumbnailRule.setRule(String.format("imageMogr2/thumbnail/%sx%s",256,256));
        thumbnailRule.setFileId(thumbKey);
        thumbnailRule.setBucket(cosClientConfig.getBucket());
        rules.add(thumbnailRule);
        }

        picOperations.setRules(rules);
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }


    /**
     * 删除对象
     *
     * @param key 文件 key
     */
    public void deleteObject(String key) throws CosClientException {
        cosClient.deleteObject(cosClientConfig.getBucket(), key);
    }


}
